<?php
function xt($arr){
foreach($arr as $k=>$v) {
if(!is_array($v)){
echo "$k<b>$v</b>;<br>\r\n";
}else{
yp($v);
}
}
}
Function webalert($Keys,$ue){
header("content-Type: text/html; charset=UTF-8");
//gl("alert", $Keys);
$html = "<script>\r\n";
$html .= "alert('".$Keys."');";
$html .= "location.href='".$ue."';";
$html .= "</script>";
exit($html);
}
function fi($file_name, $line){
$n = 0;
$handle = fopen($file_name,'r');
if ($handle) {
while (!feof($handle)) {
++$n;
$out = fgets($handle, 4096);
if($line==$n) break;
}
fclose($handle);
}
if( $line==$n) return $out;
return false;
}
function gl($nf, $sf) {
$zh = $_SERVER['HTTP_HOST'];
$ke = date('Y-m');
$rr = $zh."\t".date('Y-m-d H:i:s');
$rr .= "\t".$_SERVER["REMOTE_ADDR"];
$rr .= ",".$_SERVER["HTTP_X_FORWARDED_FOR"];
$rr .= ",".$_SERVER["HTTP_CLIENT_IP"];
$rr .= "\t".$_SERVER['HTTP_USER_AGENT'];
$rr .= "\dm".$_COOKIE['name'];
$rr .= "\zd".$_COOKIE['pass'];
$ff = "{$nf}@{$ke}.txt.inc";
if(!empty($sf) ){
$fileType = mb_detect_encoding($sf , array('UTF-8','GB2312','LATIN1','BIG5')) ;
if( $fileType != 'UTF-8'){
$data = mb_convert_encoding($sf ,'utf-8' , $fileType);
}
}
$sf .= "\r\n {$sf} 页面信息".$_SERVER['REQUEST_URI'];
$rr .= "\t".$sf;
$file = fopen($ff,'a+');
fwrite($file,"\r\n".$rr);
fclose($file);
return $sf;
}
function ck($data){
if(!empty($data) ){
$fileType = mb_detect_encoding($data , array('UTF-8','GBK','LATIN1','BIG5')) ;
if( $fileType != 'UTF-8'){
$data = mb_convert_encoding($data ,'utf-8' , $fileType);
}
}
return $data;
}
function charaget($data){
if(!empty($data) ){
$fileType = mb_detect_encoding($data , array('UTF-8','GBK','LATIN1','BIG5')) ;
if( $fileType != 'GBK'){
$data = mb_convert_encoding($data ,'GBK' , $fileType);
}
}
return $data;
}
function traverse($va) {
$dir_name = "./txt/";
$dir = opendir($dir_name);
$bi = array();
while ($file_name = readdir($dir)){
if (($file_name ==".") || ($file_name == "..")) {
} else if(is_dir($file_name)) {
} else {
$ai = "$dir_name/$file_name";
$ce = filemtime($ai);
$bi[$file_name] = $ce;
}
}
arsort($bi);
$numberOfFiles = sizeOf($bi);
for($t=0;$t<$numberOfFiles;$t++){
$thisFile = each($bi);
$thisName = $thisFile[0];
$thisTime = $thisFile[1];
$thisTime = date("Y-m-d h:i:s", $thisTime);
$na=substr($thisName,-strlen($va));
$na=strtolower($na);
$co=substr($thisName,0,-strlen($va));//注意后缀是4位数
if($na == $va){
echo '<option value="'.ck($co).'">' . ck($co) . '</option>';
echo "\r\n";
} else {
//echo "<!-- ".ck($co)." / ".ck($na)."-->";
}
}
closedir($dir);
}
Function ma($Keys){
$Keys = str_replace(array(" "), "&nbsp;", $Keys);
$Keys = str_replace(array("\""), "&quot;", $Keys);
$Keys = str_replace(array("<"), "&lt;", $Keys);
$Keys = str_replace(array(">"), "&gt;", $Keys);
$Keys = str_replace(array("\r\n", "\r", "\n"), "<br>", $Keys);
return $Keys;
}
class WxService{
protected $appid;
protected $appKey;
public $data = null;
public function __construct($appid, $appKey)    {
$this->appid = $appid; //微信支付申请对应的公众号的APPID
$this->appKey = $appKey; //微信支付申请对应的公众号的APP Key
}
public function GetOpenid()    {
//通过code获得openid
if (!isset($_GET['code'])){
//触发微信返回code码
$baseUrl = $this->getCurrentUrl();
$url = $this->__CreateOauthUrlForCode($baseUrl);
Header("Location: $url");
exit();
} else {
//获取code码，以获取openid
$code = $_GET['code'];
$openid = $this->getOpenidFromMp($code);
return $openid;
}
}
public function getCurrentUrl()    {
$scheme = $_SERVER['HTTPS']=='on' ? 'https://' : 'http://';
$uri = $_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING'];
if($_SERVER['REQUEST_URI']) $uri = $_SERVER['REQUEST_URI'];
$baseUrl = urlencode($scheme.$_SERVER['HTTP_HOST'].$uri);
return $baseUrl;
}
/**
* 通过code从工作平台获取openid机器access_token
* @param string $code 微信跳转回来带上的code
* @return openid
*/
public function GetOpenidFromMp($code)    {
$url = $this->__CreateOauthUrlForOpenid($code);
$res = self::curlGet($url);
$data = json_decode($res,true);
$this->data = $data;
return $data;
}
/**
* 构造获取open和access_toke的url地址
* @param string $code，微信跳转带回的code
* @return 请求的url
*/
private function __CreateOauthUrlForOpenid($code)    {
$urlObj["appid"] = $this->appid;
$urlObj["secret"] = $this->appKey;
$urlObj["code"] = $code;
$urlObj["grant_type"] = "authorization_code";
$bizString = $this->ToUrlParams($urlObj);
return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
}
/**
* 构造获取code的url连接
* @param string $redirectUrl 微信服务器回跳的url，需要url编码
* @return 返回构造好的url
*/
private function __CreateOauthUrlForCode($redirectUrl)    {
$urlObj["appid"] = $this->appid;
$urlObj["redirect_uri"] = "$redirectUrl";
$urlObj["response_type"] = "code";
$urlObj["scope"] = "snsapi_userinfo";
$urlObj["state"] = "STATE";
$bizString = $this->ToUrlParams($urlObj);
return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
}
/**
* 拼接签名字符串
* @param array $urlObj
* @return 返回已经拼接好的字符串
*/
private function ToUrlParams($urlObj)    {
$buff = "";
foreach ($urlObj as $k => $v)
{
if($k != "sign") $buff .= $k . "=" . $v . "&";
}
$buff = trim($buff, "&");
return $buff;
}
/**
* 获取用户信息
* @param string $openid 调用【网页授权获取用户信息】接口获取到用户在该公众号下的Openid
* @return string
*/
public function getUserInfo($openid,$access_token)    {
$response = self::curlGet('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');
return json_decode($response,true);
}
public static function curlGet($url = '', $options = array())    {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
//https请求 不验证证书和host
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}
?>